package com.smscrypt;



import java.sql.ResultSet;
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class CryptDB extends SQLiteOpenHelper {

	static final String dbName="SMSCrypt";
	static final String contactsTable="Contacts";
	static final String colID="id_contact";
	static final String colFirstName="firstname";
	static final String colLastName="lastname";
	static final String colPhoneNr="phone_nr";
	static final String colKey="crypt_key";
	static final String DEBUG_TAG = "DB Test";
		
	public CryptDB(Context context) {
		super(context, dbName, null,33);
		
		// TODO Auto-generated constructor stub
	}

	
	@Override
	public void onUpgrade(SQLiteDatabase db, int x, int y){}
	
	public void createTable() {
		// TODO Auto-generated method stub
    SQLiteDatabase db=this.getWritableDatabase();
		
		
		db.execSQL("CREATE TABLE if not exists "+contactsTable+" ("+colID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
				colFirstName+" TEXT, "+colLastName+" TEXT, "+colPhoneNr+" TEXT, "+colKey+" TEXT);");
		
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		
	}
	// nummer <Vorname Nachname>
	
	public void deleteTable(){
		SQLiteDatabase db=this.getWritableDatabase();
		db.execSQL("delete from Contacts;");
	}
	
/*	
	//alternate data insert method using raw queries	
	public void insertData(){
		
		 SQLiteDatabase db=this.getWritableDatabase();
		 db.execSQL("insert into Contacts values (1,'Hans','Janssen','049721234', 'dummycryptkey0ewef3c');");
		
	}
*/
	
	public void insertValues(String id, String fname, String lname, String pnumber, String ckey){
		
		SQLiteDatabase db=this.getWritableDatabase();
		 ContentValues cv=new ContentValues();
		   
		   cv.put(colID, id);
		   cv.put(colFirstName, fname);
		   cv.put(colLastName, lname);
		   cv.put(colPhoneNr, pnumber);
		   cv.put(colKey, ckey);
		   db.insert(contactsTable, colID, cv);
		                     db.close();
	}
	
	Cursor sqlGetContactList(){
		
			 SQLiteDatabase db=this.getWritableDatabase();
			 Cursor cur= db.rawQuery("SELECT "+ colPhoneNr + "," + colFirstName + "," + colLastName + " FROM "+contactsTable,null);

			 return cur;
			 
	}
	
	public void logCursorInfo(Cursor c) {
		ArrayList<String> mArrayList = new ArrayList<String>();
		// Print records
		c.moveToFirst();
		while (c.isAfterLast() == false) {
		String rowResults = "";
		for (int i = 0; i < c.getColumnCount(); i++) {
		rowResults = rowResults.concat(c.getString(i) + " ");
		}
		Log.i(DEBUG_TAG, rowResults);
		c.moveToNext();
		mArrayList.add(rowResults);
		//c.close();
		}
		c.close();
		//System.out.println(mArrayList);
		}
	
	//Array list returns phone number and name. Example: "01234567789 John Doe"
	public static ArrayList Contacts(ArrayList mArrayList){
		return mArrayList;
		
	}

	
	
	
	/* void AddEmployee(Employee emp)
	{
		 
		 
		 SQLiteDatabase db= this.getWritableDatabase();
		 
		
		ContentValues cv=new ContentValues();
		
		cv.put(colName, emp.getName());
		cv.put(colAge, emp.getAge());
		cv.put(colDept, emp.getDept());
		//cv.put(colDept,2);
		
		db.insert(contactsTable, colName, cv);
		db.close();
		
		
	}*/
	 
	 /*int getEmployeeCount()
	 {
		SQLiteDatabase db=this.getWritableDatabase();
		Cursor cur= db.rawQuery("Select * from "+contactsTable, null);
		int x= cur.getCount();
		cur.close();
		return x;
	 }*/
	 /* 
	 Cursor getAllEmployees()
	 {
		 SQLiteDatabase db=this.getWritableDatabase();
		 
		 
		 
		 //Cursor cur= db.rawQuery("Select "+colID+" as _id , "+colName+", "+colAge+" from "+contactsTable, new String [] {});
		 Cursor cur= db.rawQuery("SELECT * FROM "+viewEmps,null);
		 return cur;
		 
	 }*/
	 
	/* 
	 void InsertDepts(SQLiteDatabase db)
	 {
		 ContentValues cv=new ContentValues();
			cv.put(colDeptID, 1);
			cv.put(colDeptName, "Sales");
			db.insert(deptTable, colDeptID, cv);
			cv.put(colDeptID, 2);
			cv.put(colDeptName, "IT");
			db.insert(deptTable, colDeptID, cv);
			cv.put(colDeptID, 3);
			cv.put(colDeptName, "HR");
			db.insert(deptTable, colDeptID, cv);
			db.insert(deptTable, colDeptID, cv);
			
	 }*/
	 /* 
	 public String GetDept(int ID)
	 {
		 SQLiteDatabase db=this.getReadableDatabase();
		 
		 String[] params=new String[]{String.valueOf(ID)};
		 Cursor c=db.rawQuery("SELECT "+colDeptName+" FROM"+ deptTable+" WHERE "+colDeptID+"=?",params);
		 c.moveToFirst();
		 int index= c.getColumnIndex(colDeptName);
		 return c.getString(index);
	 }*/
	 /* 
	 public Cursor getEmpByDept(String Dept)
	 {
		 SQLiteDatabase db=this.getReadableDatabase();
		 String [] columns=new String[]{"_id",colName,colAge,colDeptName};
		 Cursor c=db.query(viewEmps, columns, colDeptName+"=?", new String[]{Dept}, null, null, null);
		 return c;
	 }*/
	 /* 
	 public int GetDeptID(String Dept)
	 {
		 SQLiteDatabase db=this.getReadableDatabase();
		 Cursor c=db.query(deptTable, new String[]{colDeptID+" as _id",colDeptName},colDeptName+"=?", new String[]{Dept}, null, null, null);
		 //Cursor c=db.rawQuery("SELECT "+colDeptID+" as _id FROM "+deptTable+" WHERE "+colDeptName+"=?", new String []{Dept});
		 c.moveToFirst();
		 return c.getInt(c.getColumnIndex("_id"));
		 
		 }*/
	 
	 /*public int UpdateEmp(Employee emp)
	 {
		 SQLiteDatabase db=this.getWritableDatabase();
		 ContentValues cv=new ContentValues();
		 cv.put(colName, emp.getName());
		 cv.put(colAge, emp.getAge());
		 cv.put(colDept, emp.getDept());
		 return db.update(contactsTable, cv, colID+"=?", new String []{String.valueOf(emp.getID())});
		 
	 }*/
	 
	 /*public void DeleteEmp(Employee emp)
	 {
		 SQLiteDatabase db=this.getWritableDatabase();
		 db.delete(contactsTable,colID+"=?", new String [] {String.valueOf(emp.getID())});
		 db.close();
		 
		
		
	 }*/

}
